<?php
namespace app\api\controller;
use app\common\controller\Api;

/**
 * 用户个人信息
 */
class Userinfo extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    
//获取个人信息
    public function index(){
        $userInfo = $this->auth->getUserinfo();
        $user_id = $userInfo['id'];
        $user_arr = db('user')->where('id', $user_id)->find();
		//未审批的订单数量
	    $count = db('userinfo_checklog')->field("id")->where('user_id',$user_id)->where('status',0)->count();
        $data=array(
            'id'=>$user_arr['id'],
            'avatar'=>$user_arr['avatar'],
            'nickname'=>$user_arr['nickname'],
            'isauth'=>$user_arr['isauth'],
            //'ishr'=>$user_arr['ishr'],
			'type'=>$user_arr['type'],
            'company_name'=>db('company_member')->where('id', $user_arr['company_id'])->find()['company'],
            'zhicheng'=>$user_arr['zhicheng'],
			'company_address'=>$user_arr['company_address'],
			'count'=>$count
        );
        $this->success('success',['data'=>$data]);
    }
	
	
	
    
    //修改资料
    public function editUser(){
        $userInfo = $this->auth->getUserinfo();
        $user_id = $userInfo['id'];
        $zhicheng= $this->request->request("zhicheng");
        $company_address= $this->request->request("company_address");
        $nickname = $this->request->request("nickname");
		if(!$zhicheng || !$company_address || !$nickname){
			$this->error("缺少必要参数");
		}
        if($zhicheng){
            $data['zhicheng']=$zhicheng;
        }
        if($company_address){
            $data['company_address']=$company_address;
        }
		if($nickname){
			$data['nickname']=$nickname;
		}
        $res = db('user')->where('id', $user_id)->update($data);
		if($res){
			$this->success('信息修改成功');
		}else{
			$this->error('信息修改失败，请重新操作');
		}
    }
	
	
	
	
	//获取单位管理信息
    public function getDanwei(){
        $userInfo = $this->auth->getUserinfo();
		$list = db('user_danwei')->field("*")->where('user_id',$userInfo['id'])->find();
       	$this->success('获取单位信息成功',['data'=>$list]);
	}
	
	
	
	//修改单位管理信息
	public function editDanwei(){
        $userInfo = $this->auth->getUserinfo();
        $name= $this->request->request("name");
        $mobile= $this->request->request("mobile");
        $contact_person= $this->request->request("contact_person");
		if(!$name && !$mobile && !$contact_person){
			$this->error('缺少参数');
		}
	    if($name){
			$data['name']=$name;
		}
	    if($mobile){
			$data['mobile']=$mobile;
		}
	    if($contact_person){
			$data['contact_person']=$contact_person;
		}
		$id = db('user_danwei')->field("id")->where('user_id',$userInfo['id'])->find();
		if(!$id){
			$data['user_id'] = $userInfo['id'];
			$data['createtime'] = time();
			$res = db('user_danwei')->insert($data);
			$msg = '添加';
		}else{
        	$res = db('user_danwei')->where('user_id', $userInfo['id'])->update($data);
			$msg = '修改';
		}
        if($res){
			$this->success($msg.'成功');
		}else{
			$this->error($msg."失败,请重新操作");
		}
	}
	
	
	
    
	
	
	
	
	
	
	
    
}

